<?php

/* 
 * 西联接口API
 * @Author wyb
 */
namespace DataAPI\Extend;
class Xilian {
    
    public static $_self = null;
    
    public $appKey = '';
    public $registerKey = '';
    public $_result_field = 'data';
    public $_model = '';
    public static $_error = '';
    
    public static function init() {
        return self::$_self == null ? self::$_self = new self() : self::$_self;
    }

    function __construct() {
        $this->url="http://113.204.37.210:6066/openErp.asmx";
        $this->appKey = '0011';
        $this->registerKey = '2CCF-F2H8-9ABD-6FA7-9803';
    }

    /**
     * 注册并取得授权
     * @return boolean
     */
    public function register_auth() {
        $parm = array(
            'appKey' => $this->appKey,
            'registerKey' => $this->registerKey,
            'Sign' => $this->make_sign($this->appKey . $this->registerKey)
        );
        $this->_model = 'RegisterAuth';
        $result = $this->getSendData($parm);
        if( $result !== false ) {
            return true;
        } else {
            return false;
        }
    }
    
    /**
     * 获取门店商品资料
     * @param type $organ
     * @param type $page
     */
    public function get_commdity_shop($organ = '0000', $page = 1) {
        $parm = array(
            'appKey' => $this->appKey,
            'pageIndex' => $page,
            'pageSize' => 100,
            'Organ' => $organ
        );
        $parm['sign'] = $this->make_sign(implode('', $parm));
        $this->_model = 'GetCommdityShop';
        $this->_result_field = 'CommShopList';
        $result = $this->getSendData($parm);
        if( $result !== false ) {
            return( $result );
        } else {
            return false;
        }
    }
    
    /**
     * 获取门店信息
     */
    public function get_shop() {
        $parm = array(
            'appKey' => $this->appKey
        );
        $parm['sign'] = $this->make_sign(implode('', $parm));
        $this->_model = 'GetShop';
        $this->_result_field = 'organList';
        $result = $this->getSendData($parm);
        if( $result !== false ) {
            return $result;
        } else {
            return false;
        }
    }
    
    /**
     * 获取已删除门店信息
     */
    public function get_deleted_shop() {
        $parm = array(
            'appKey' => $this->appKey
        );
        $parm['sign'] = $this->make_sign(implode('', $parm));
        $this->_model = 'GetDeletedShop';
        $this->_result_field = 'organList';
        $result = $this->getSendData($parm);
        if( $result !== false ) {
            return( $result );
        } else {
            return false;
        }
    }
    
    /**
     * 获取商品信息
     * @param int $page
     */
    public function get_commodity( $page = 1 ){
        $parm = array(
            'appKey' => $this->appKey,
            'pageIndex' => $page,
            'pageSize' => 100
        );
        $parm['sign'] = $this->make_sign(implode('', $parm));
        $this->_model = 'GetCommodity';
        $this->_result_field = 'commodityList';
        $result = $this->getSendData($parm);
        if( $result !== false ) {
            return $result;
        } else {
            return false;
        }
    }
    
    /**
     * 获取商品详细信息
     * @param string $code
     */
    public function get_commodity_info( $code ){
        $parm = array(
            'appKey' => $this->appKey,
            'code' => $code
        );
        $parm['sign'] = $this->make_sign(implode('', $parm));
        $this->_model = 'GetCommodityInfo';
        $this->_result_field = 'commodity';
        $result = $this->getSendData($parm);
        if( $result !== false ) {
            return( $result );
        } else {
            return false;
        }
    }
    
    /**
     * 获取已删除商品信息
     */
    public function get_deleted_commodity() {
        $parm = array(
            'appKey' => $this->appKey
        );
        $parm['sign'] = $this->make_sign(implode('', $parm));
        $this->_model = 'GetDeletedCommodity';
        $this->_result_field = 'commodityList';
        $result = $this->getSendData($parm);
        if( $result !== false ) {
            return( $result );
        } else {
            return false;
        }
    }
    
    /**
     * 获取商品捆绑销售信息
     */
    public function get_commodity_binding( $page = 1 ){
        $parm = array(
            'appKey' => $this->appKey,
            'pageIndex' => $page,
            'pageSize' => 100
        );
        $parm['sign'] = $this->make_sign(implode('', $parm));
        $this->_model = 'GetCommodityBinding';
        $this->_result_field = 'commodityList';
        $result = $this->getSendData($parm);
        if( $result !== false ) {
            return( $result );
        } else {
            return false;
        }
    }
    
    /**
     * 获取商品类别资料
     */
    public function get_sort( $page = 1 ){
        $parm = array(
            'appKey' => $this->appKey,
            'pageIndex' => $page,
            'pageSize' => 100
        );
        $parm['sign'] = $this->make_sign(implode('', $parm));
        $this->_model = 'GetSort';
        $this->_result_field = 'sortList';
        $result = $this->getSendData($parm);
        if( $result !== false ) {
            return( $result );
        } else {
            return false;
        }
    }
    
    /**
     * 获取品牌资料
     */
    public function get_brands( $page = 1 ){
        $parm = array(
            'appKey' => $this->appKey,
            'pageIndex' => $page,
            'pageSize' => 100
        );
        $parm['sign'] = $this->make_sign(implode('', $parm));
        $this->_model = 'GetBrands';
        $this->_result_field = 'brandList';
        $result = $this->getSendData($parm);
        if( $result !== false ) {
            return( $result );
        } else {
            return false;
        }
    }
    
    /**
     * 商品价格查询
     * @param type $organ
     * @param type $code
     */
    public function query_price( $code , $organ ){
        $parm = array(
            'appKey' => $this->appKey,
            'Organ' => $organ,
            'Code' => $code,
            'PriceType' => 1
        );
        $parm['Sign'] = $this->make_sign(implode('', $parm));
        $this->_model = 'QueryPrice';
        $this->_result_field = array( 'Price' , 'OrigPrice' );
        $result = $this->getSendData($parm);
        if( $result !== false ) {
            return $result;
        } else {
            return false;
        }
    }
    
    /**
     * 获取商品价格及可用库存
     * @param type $code
     * @param type $organ
     */
    public function query_stock_and_price($code , $organ){
        $parm = array(
            'appKey' => $this->appKey,
            'Organ' => $organ,
            'Code' => $code,
            'PriceType' => 1
        );
        
        $parm['sign'] = $this->make_sign(implode('', $parm));
        $this->_model = 'QueryStockAndPrice';
        $this->_result_field = array( 'Price' , 'OrigPrice' ,'Stock' );
        $result = $this->getSendData($parm);
        if( $result !== false ) {
            return( $result );
        } else {
            return false;
        }
    }
    
    /**
     * 获取同一门店多个商品价格及可用库存
     * @param type $codes
     * @param type $organ
     */
    public function query_stock_and_price_by_codes( $codes , $organ ){
        $parm = array(
            'appKey' => $this->appKey,
            'Organ' => $organ,
            'Codes' => $codes,
            'PriceType' => 1
        );
        $parm['sign'] = $this->make_sign(implode('', $parm));
        $this->_model = 'QueryStockAndPrice';
        $this->_result_field = 'StockList';
        $result = $this->getSendData($parm);
        if( $result !== false ) {
            return( $result );
        } else {
            return false;
        }
    }
    
    /**
     * 商品即时库存查询
     * @param type $code
     * @param type $organ
     */
    public function query_stock($code , $organ){
        $parm = array(
            'appKey' => $this->appKey,
            'Organ' => $organ,
            'Code' => $code
        );
        $parm['Sign'] = $this->make_sign(implode('', $parm));
        $this->_model = 'QueryStock';
        $this->_result_field = 'Stock';
        $result = $this->getSendData($parm);
        if( $result !== false ) {
            return( $result );
        } else {
            return false;
        }
    }
    
    /**
     * 商品账库存查询
     * @param type $page
     */
    public function query_account_stock( $page = 1 ){
        $parm = array(
            'appKey' => $this->appKey,
            'pageIndex' => $page,
            'pageSize' => 100
        );
        $parm['sign'] = $this->make_sign(implode('', $parm));
        $this->_model = 'QueryAccountStock';
        $this->_result_field = 'StockList';
        $result = $this->getSendData($parm);
        if( $result !== false ) {
            return( $result );
        } else {
            return false;
        }
    }
    
    /**
     * 获取系统付款方式
     */
    public function query_payment_type(){
        $parm = array(
            'appKey' => $this->appKey
        );
        $parm['sign'] = $this->make_sign(implode('', $parm));
        $this->_model = 'QueryPaymentType';
        $this->_result_field = 'PaymentList';
        $result = $this->getSendData($parm);
        if( $result !== false ) {
            return( $result );
        } else {
            return false;
        }
    }
    
    /**
     * 发送请求并获得数据
     * @param type $parm
     * @return boolean
     */
    public function getSendData( $parm ) {
        $result = file_get_contents( $this->url . '/' .$this->_model .'?' . http_build_query($parm));
        $result = (array) simplexml_load_string($result, 'SimpleXMLElement', LIBXML_NOCDATA);
        $result = json_decode(base64_decode($result[0]), true);
        if (isset($result['success']) && $result['success'] == 'True') {
            if( is_array( $this->_result_field ) ) {
                foreach( $this->_result_field as $k => $v ) {
                    $data[$v] = $result['data'][$v];
                }
            } else {
                $data = $result['data'][$this->_result_field];
            }
            return array('msg' => $result['message'] , 'data' => $data);
        } else {
            self::$_error = $result['message'];
            return false;
        }
    }
    
    /**
     * 获得错误信息
     */
    public function getError() {
        return self::$_error;
    }

    private function make_sign($str) {
        $tmp = md5(strtoupper(md5($str)));
        return substr($tmp, 0, strlen($tmp) - 3);
    }

    public function decode($str, $key = '') {
        $strBin = base64_decode($str);
        return $strBin;
    }
}
